#!/usr/bin/python3

import os.path
import tkinter as tk
import tkinter.filedialog
import tkinter.messagebox
from tkinter import *

import cv2
import pymysql
import qrcode
from PIL import Image, ImageTk

# 建立零时文件保存登录信息
path = os.getenv('temp')
filename = os.path.join(path, 'info.txt')

window = tk.Tk()
window.title('贝宝拉·')# 窗口标题
window.geometry('500x300')# 窗口大小
# window.iconbitmap('d:\logo.ico')# logo
window.resizable(width=False,height=False)# 窗口大小固定

username = tk.Entry(window,width=20)#用户名输入框
username.pack()
password = tk.Entry(window,width=20,show='*')#密码输入框
password.pack()

def connect_db():#连接数据库检验密码
    db_name = username.get()# 取用户名
    db_password = password.get()# 取密码
    # db_name = '1'
    # db_password = '1'
    conn = pymysql.connect(host='数据库地址', user='root', passwd='数据库密码', port=3306, db='数据库',charset='utf8')
    cursor = conn.cursor()    # 使用 cursor() 方法创建一个游标对象 cursor
    results1 = cursor.execute("SELECT * FROM `wx` WHERE `idphone` LIKE '"+db_name+"' AND `password` LIKE '"+db_password+
                              "';")# 查询账号密码是否在数据库中，使用 execute() 方法执行 SQL 查询
    if results1 != 1:
        tk.messagebox.showwarning('警告', '用户名或密码不正确')
    else:
        with open(filename, 'w') as fp:#保存登录信息
            fp.write(','.join((db_name, db_password)))
        main_page()
    cursor.close()    # 关闭游标，又从起始位置开始
    conn.close()    # 关闭数据库连接
button = tk.Button(window,text='登录',command=lambda:connect_db())#登录按钮
button.pack() #登录

def selectPath():#上传QR码，解码重新打包QR码
    print('111')
    file_name = tk.filedialog.askopenfilename()
    d = cv2.QRCodeDetector()
    val, _, _ = d.detectAndDecode(cv2.imread(file_name))
    wx = 'wx'
    zfb = 'alipay'
    if wx in val:
        with open(filename) as fp:
            myname, mypassword = fp.read().strip().split(',')
        db_name = myname
        conn = pymysql.connect(host='数据库地址', user='root', passwd='数据库密码', port=3306, db='数据库',charset='utf8')
        cursor = conn.cursor()  # 使用 cursor() 方法创建一个游标对象 curso
        results1 = ("UPDATE `wx` SET `wx_url`='"+val+"' WHERE `idphone` LIKE "+db_name+";")
        try:
            cursor.execute(results1)
            conn.commit()
            print('成功')
            tk.messagebox.showinfo('写入成功', '微信二维码Data写入成功')
        except Exception as e:
            print("失败：case%s"%e)
            conn.rollback()
        finally:
            # 关闭游标，又从起始位置开始
            cursor.close()
            # 关闭数据库连接
            conn.close()

    elif zfb in val:
        img = qrcode.make(val)
        with open(filename) as fp:
            myname, mypassword = fp.read().strip().split(',')
        db_name = myname
        conn = pymysql.connect(host='数据库地址', user='root', passwd='数据库密码', port=3306, db='数据库',charset='utf8')
        cursor = conn.cursor()  # 使用 cursor() 方法创建一个游标对象 cursor

        results1 = ("UPDATE `wx` SET `zfb_url`='"+val+"' WHERE `idphone` LIKE "+db_name+";")
        # UPDATE `wx` SET `wx_url`= '11111' WHERE `idphone` LIKE '13000000000'
        try:
            cursor.execute(results1)
            conn.commit()
            print('成功')
            tk.messagebox.showinfo('写入成功','支付宝二维码Data写入成功')
        except Exception as e:
            print("失败：case%s"%e)
            conn.rollback()
        finally:
            # 关闭游标，又从起始位置开始
            cursor.close()
            # 关闭数据库连接
            conn.close()
        img.save("QR/zfb.jpg")
    else:
        tk.messagebox.showwarning('警告', '请选择微信或支付宝收款码')

def flush_data():
    print('111')

def show():
    img = Image.open('QR/wx.jpg')  # 打开图片
    photo = ImageTk.PhotoImage(img)  # 用PIL模块的PhotoImage打开
    imglabel = tk.Label(main_page, image=photo)
    imglabel.grid(row=0, column=0, columnspan=3)
    print('1111')

def main_page():
    window.destroy()
    main_page = tk.Tk()
    main_page.title('贝宝拉·')  # 窗口标题
    main_page.geometry('600x600')  # 窗口大小
    # window.iconbitmap('d:\logo.ico')# logo
    main_page.resizable(width=False, height=False)  # 窗口大小固定
    with open(filename) as fp:
        myname, mypassword = fp.read().strip().split(',')
    user_name = ('用户名:'+myname)
    lb = tk.Label(main_page, text=user_name,  # 设置文本内容
                  justify='left',  # 设置文本对齐方式：左对齐
                  anchor='nw',  # 设置文本在label的方位：西北方位
                  font=('微软雅黑', 18), ) # 设置字体：微软雅黑，字号：18
    lb.pack()
    #显示二维码
    QR_wxphoto = Image.open('QR/wxa.jpg')
    QR_wxphoto = QR_wxphoto.resize((200, 180))  # 规定图片大小
    QR_wxphoto = ImageTk.PhotoImage(QR_wxphoto)
    QR_zfbphoto = Image.open('QR/zfba.jpg')
    QR_zfbphoto = QR_zfbphoto.resize((200, 180))  # 规定图片大小
    QR_zfbphoto = ImageTk.PhotoImage(QR_zfbphoto)
    QR_bblphoto = Image.open('QR/bbla.jpg')
    QR_bblphoto = QR_bblphoto.resize((200, 180))  # 规定图片大小
    QR_bblphoto = ImageTk.PhotoImage(QR_bblphoto)
    label_wx = tk.Label(main_page, image=QR_wxphoto, text='微信', compound="top", bg="lightgreen", width=200, height=200)
    label_wx.place(x=50, y=50)
    label_zfb = tk.Label(main_page, image=QR_zfbphoto, text='支付宝', compound="top", bg="lightblue")
    label_zfb.place(x=350, y=50)
    label_wx_zfb = tk.Label(main_page, image=QR_bblphoto, text='贝拉宝', compound="top", bg="lightblue")
    label_wx_zfb.place(x=50, y=300)#位置
    up_photo = tk.Button(main_page, text="上传·QR码", command=lambda:selectPath)
    up_photo.place(x=420, y=320)
    up_photo = tk.Button(main_page, text="生成·贝宝拉", command=selectPath)
    up_photo.place(x=420, y=360)
    up_photo = tk.Button(main_page, text="刷新·数据", command=show)
    up_photo.place(x=420, y=400)
    main_page.mainloop()





window.mainloop()